package com.lnking.lnkingplace.Util;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeUtil {

    /**
     * 日期处理-String转LocalDateTime yyyy-MM-dd HH:mm:ss
     *
     * @param
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String dateTime,String format) {
        LocalDateTime nowDateTime = LocalDateTime.now();
        if (StringUtils.isBlank(dateTime)) {
            return nowDateTime;
        }

        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        LocalDateTime ldt = LocalDateTime.parse(dateTime, df);
        return ldt;
    }

    /**
     * 日期处理-String转LocalDate yyyy-MM-dd
     * @param date
     * @return
     */
    public static LocalDate stringToLocalDate(String date,String format){
        //当前时间
        LocalDate nowDate = LocalDate.now();
        if (StringUtils.isBlank(date)) {
            return nowDate;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ld = LocalDate.parse(date, df);

        return ld;
    }

    /**
     * LocalDateTime转String - 年月日
     * @param dateTime
     * @return
     */
    public static String localDateTimeToString(LocalDateTime dateTime,String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        String localTime = df.format(dateTime);
        return localTime;
    }

    /**
     * LocalDate转String - 年月日
     * @param dateTime
     * @return
     */
    public static String localDateToString(LocalDate dateTime,String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        String localTime = df.format(dateTime);
        return localTime;
    }

    //如果在今天之内，返回true，否则返回false
    public static boolean isOnToday(LocalDateTime paramTime){
        boolean result = false;
        //今天开始时间0时0分0秒
        LocalDateTime todayStartTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //今天结束时间23时59分59秒
        LocalDateTime todayEndTime = LocalDateTime.of(LocalDate.now(),LocalTime.MAX);
        //a.isAfter(b) a在b时间之后   a.isBefore(b) a在b时间之前
        if(paramTime.isAfter(todayStartTime) && paramTime.isBefore(todayEndTime )){
            result = true;
        }else if(paramTime.equals(todayStartTime ) || paramTime.equals(todayStartTime )){
            result = true;
        }
        return result;
    }
}
